using System.Collections;
using UnityEngine;

public class NoInternetPopup : MonoBehaviour
{
	private GameObject panel;

	public static NoInternetPopup instance;

	private bool _internetOff = true;

	private bool checking;

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		else if (instance != this)
		{
			Object.Destroy(base.gameObject);
		}
		panel = GameObject.Find("NoInternetPopUp");
		Object.DontDestroyOnLoad(base.gameObject);
	}

	public void LevelChanged()
	{
		if (panel == null)
		{
			panel = GameObject.Find("NoInternetPopUp");
		}
		panel.SetActive(value: false);
	}

	private void Start()
	{
		StopAllCoroutines();
		if (panel == null)
		{
			panel = GameObject.Find("NoInternetPopUp");
		}
		panel.SetActive(value: false);
		StartCoroutine(CheckInternet());
	}

	private IEnumerator CheckInternet()
	{
		while (true)
		{
			if (checking)
			{
				yield return null;
				continue;
			}
			yield return new WaitForSeconds(_internetOff ? 1f : 3f);
			if (panel == null)
			{
				panel = GameObject.Find("NoInternetPopUp");
				continue;
			}
			_internetOff = Application.internetReachability == NetworkReachability.NotReachable;
			if (_internetOff)
			{
				StartCoroutine(CheckAndShow());
			}
			else
			{
				panel.SetActive(value: false);
			}
		}
	}

	private IEnumerator CheckAndShow()
	{
		checking = true;
		float timer = 0f;
		while (timer < 1f)
		{
			yield return null;
			timer += Time.deltaTime;
			if (Application.internetReachability != 0)
			{
				break;
			}
		}
		if (Application.internetReachability == NetworkReachability.NotReachable)
		{
			panel.SetActive(value: true);
		}
		checking = false;
	}
}
